﻿using System;
using System.Data;
using System.Collections;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace Jungle.Code.Utils
{
	public class UrlHelper
	{
		// Constructors
		private UrlHelper()
		{
		}


		// Methods
		public static string GetApplicationPath()
		{
			string str0 = HttpContext.Current.Request.ApplicationPath;
			if (str0.EndsWith("/"))
			{
				return str0;
			}
			return str0 + "/";
		}

		public static string GetSiteUrl()
		{
			string Port = System.Web.HttpContext.Current.Request.ServerVariables["SERVER_PORT"];
			if (Port == null || Port == "80" || Port == "443")
				Port = "";
			else
				Port = ":" + Port;

			string Protocol = System.Web.HttpContext.Current.Request.ServerVariables["SERVER_PORT_SECURE"];
			if (Protocol == null || Protocol == "0")
				Protocol = "http://";
			else
				Protocol = "https://";

			string appPath = System.Web.HttpContext.Current.Request.ApplicationPath;
			if (appPath == "/")
				appPath = "";

			string sOut = Protocol + System.Web.HttpContext.Current.Request.ServerVariables["SERVER_NAME"] + Port + appPath;
			return sOut;
		}

		public static string GetAdminUrl()
		{
			string text1 = HttpContext.Current.Request.ApplicationPath;
			if (text1.EndsWith("/") && (text1.Length == 1)){

                return UrlHelper.GetHostUrl() + "/admin/";
			}
			return UrlHelper.GetHostUrl() + text1.ToLower() + "/admin/";
		}

		public static string[] GetVirtuals()
		{
			string url = GetRequestUrl();

            if (url.IndexOf("ru/guide/") != -1)
            {
                url = url.Substring(0, 8);
            }
            else
            {

                if (url.Length <= 0)
                {
                    return null;
                }
                if (url.EndsWith("/"))
                {
                    url = url.Substring(0, url.Length - 1);
                }
                if (url.StartsWith("/"))
                {
                    url = url.Substring(1, url.Length - 1);
                }
                if (url.IndexOf("/main.aspx") != -1)
                {
                    url = url.Substring(0, url.IndexOf("/main.aspx"));
                }
                if (url.IndexOf(".aspx") != -1)
                {
                    url = url.Substring(0, url.IndexOf(".aspx"));
                }
            }
			return url.Split(new char[] { '/' });
		}

		public static string GetRequestUrl()
		{
			string text1 = (string)HttpContext.Current.Items["VirtualUrl"];
			text1 = text1.ToLower();
			string value = UrlHelper.GetApplicationPath();
			if ((value.Length > 1) && (text1.IndexOf(value) != -1))
			{
				return text1.Substring(text1.IndexOf(value) + value.Length);
			}
			return text1.Substring(1);
		}

		private static string GetHostUrl()
		{
			string text1 = HttpContext.Current.Request.ServerVariables["SERVER_PORT_SECURE"];
			string arg0 = ((text1 == null) || (text1 == "0")) ? "http" : "https";
			string str1 = HttpContext.Current.Request.ServerVariables["SERVER_PORT"];
			string arg2 = (str1 == "80") ? string.Empty : (":" + str1);
			string arg1 = HttpContext.Current.Request.ServerVariables["SERVER_NAME"];
			return string.Format("{0}://{1}{2}", arg0, arg1, arg2);
		}

		public static string FormatUrl(string page, params Arg[] args)
		{
			string url = GetAdminUrl() + page;
			if (args.Length > 0)
			{
				url += "?" + args[0].Key + "=" + args[0].Value;
				for (int i = 1; i < args.Length; i++)
					url += "&" + args[i].Key + "=" + args[i].Value;
			}
			return url;
		}


	}
}
